package com.platform.util;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.platform.common.ServerResponse;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;

public class SearchConsumablesUtil {

    private static Logger log = LoggerFactory.getLogger(SearchConsumablesUtil.class);
    private static HttpSolrServer solrServer = new HttpSolrServer(PropertiesUtil.getProperty("solr.url"));
    private static int maxSize = 300;

    /**
     * 查询对应大字段
     * @return
     */
    public static ServerResponse<PageInfo> selectBigText(PageInfo pageInfo){
        List<Map> mapList = pageInfo.getList();
        try {
            if(null != mapList && 0 < mapList.size()){

                for (int i = 0; i < mapList.size() ; i++) {
                    //根据ID查询对应大字段
                    //查询
                    SolrQuery params = new SolrQuery("eid:"+mapList.get(i).get("id"));
                    //指定显示域
                    String[] fields = {"specifications","model"};
                    params.setFields(fields);
                    QueryResponse response = solrServer.query(params,SolrRequest.METHOD.POST);
                    //返回值
                    SolrDocumentList list = response.getResults();
                    if(null != list && 0 < list.size()){
                        for (SolrDocument documents : list) {
                            mapList.get(i).put("model",
                                    documents.getFieldValue("model"));
                            mapList.get(i).put("specifications",
                                    documents.getFieldValue("specifications"));
                        }
                    }
                }
                pageInfo.setList(mapList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.createBySuccess(pageInfo);
    }

    /**
     * 以大字段为搜索条件，查询符合条件ids
     * @param paramMap
     * @return
     */
    public static List<Integer> selectDataByBigFields(Map paramMap){
        List<Integer> ids = Lists.newArrayList();
        //获取参数
        try {
            StringBuilder qf = new StringBuilder();
            String parStr = StringUtil.valueOfString(paramMap.get("sm"));
            if(StringUtil.isNotEmpty(parStr)){
                qf.append("specifications:"+parStr +"OR model:"+parStr);
            }
            String productname = StringUtil.valueOfString(paramMap.get("productname"));
            if(StringUtil.isNotEmpty(productname)){
                if(StringUtil.isEmpty(qf.toString())){
                    qf.append("productname:"+productname);
                }else{
                    qf.append("AND productname:"+productname);
                }
            }
            String mb = StringUtil.valueOfString(paramMap.get("mb"));
            if(StringUtil.isNotEmpty(mb)){
                if(StringUtil.isEmpty(qf.toString())){
                    qf.append("manufacture:"+mb);
                }else{
                    qf.append("AND manufacture:"+mb);
                }
            }
            //设置条件值
            SolrQuery params = new SolrQuery(qf.toString()+"defType=edismax&mm=60%25");
            params.setStart(0);
            params.setRows(20);//最多条目数
            //设置显示域
            String[] fields = {"eid"};
            params.setFields(fields);
            //查询
            QueryResponse response = solrServer.query(params);
            SolrDocumentList list = response.getResults();
            for (int i=0;i<list.size();i++){
                ids.add(NumberUtil.safeToNumber(list.get(i).get("eid")));
            }

        } catch (Exception e) {
            log.error(e.toString());
        }
        //剔除重复元素
        HashSet h = new HashSet(ids);
        ids.clear();
        ids.addAll(h);
        return ids;
    }


    /**
     * 根据条件搜索数据
     * @param paramMap
     * @return
     */
    public static SolrDocumentList selectByParamMap(Map paramMap){
        try{
            StringBuilder qf = new StringBuilder();
            String productname = StringUtil.valueOfString(paramMap.get("productname")).trim();
            if(StringUtil.isNotEmpty(productname)){
                qf.append("productname:\\"+productname+"\\"+"^11.0");
            }
            String sm = StringUtil.valueOfString(paramMap.get("sm")).trim();
            if(StringUtil.isNotEmpty(sm)){
                if(StringUtil.isEmpty(qf.toString())){
                    qf.append("specifications:\\"+sm +"\\OR model:\\"+sm+"^11.0");
                    //qf.append("model:\\"+sm);
                }else{
                    qf.append("AND specifications:\\"+sm +"\\OR model:\\"+sm+"^11.0");
                }
            }
            String mb = StringUtil.valueOfString(paramMap.get("mb")).trim();
            if(StringUtil.isNotEmpty(mb)){
                if(StringUtil.isEmpty(qf.toString())){
                    qf.append("manufacture:\\"+mb+"\\");
                }else{
                    qf.append("AND manufacture:\\"+mb+"\\");
                }
            }
            //设置条件值
            SolrQuery params = new SolrQuery(qf.toString()+"defType=edismax&mm=60%");
            params.setStart(0);
            params.setRows(maxSize);//最多条目数
            //设置显示域
            String[] fields = {"eid","productname","specifications","model","manufacture","registration_number",
                    "price","datahome","datatime","bid_company"};
            params.setFields(fields);
            //查询
            QueryResponse response = solrServer.query(params);
            SolrDocumentList list = response.getResults();
            if(null != list && 0 < list.size()){
                for (int i = 0; i < list.size(); i++) {
                    if(StringUtil.isNotEmpty(list.get(i).get("datatime"))){
                        list.get(i).setField("datatime",list.get(i).get("datatime").toString().split("T")[0]);
                    }
                }
                return list;
            }
        }catch (Exception e){
            log.error(e.toString());
        }
        return null;
    }

}
